package com.orangelife.service.bill;

import java.io.File;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;

import org.kie.api.KieServices;
import org.kie.api.builder.KieBuilder;
import org.kie.api.builder.KieFileSystem;
import org.kie.api.builder.KieRepository;
import org.kie.api.runtime.KieContainer;
import org.kie.api.runtime.KieSession;

import com.orangelife.service.drool.BillParms;

/**
 * This is a sample class to launch a rule.
 */
public class Test {

    public static final void main(String[] args) {
    	  try {
      		
              // load up the knowledge base
  	        KieServices ks = KieServices.Factory.get();
//  			ks.newKieBuilder();
  			KieRepository kr =ks.getRepository();
  			KieFileSystem kfs=ks.newKieFileSystem();
  		kfs.write("src/main/resources/rules1/Sample.drl",rule());
  File f=	new File(ClassLoader.getSystemResource("rules1/Sample3.drl").toURI());
  System.out.println(f.getName());
//  		System.out.println(ClassLoader.getSystemResource("rules1/Sample.drl"));
  	
  		KieServices ks1 = KieServices.Factory.get();
//		ks.newKieBuilder();
		KieRepository kr1 =ks.getRepository();
		KieFileSystem kfs1=ks.newKieFileSystem();
	kfs1.write(ks1.getResources().newClassPathResource("rules1/Sample3.drl"));
	;

	
	KieBuilder kb1=ks1.newKieBuilder(kfs1);

		kb1.buildAll();
		
	    KieContainer kContainer1 = ks1.newKieContainer(kr1.getDefaultReleaseId());
     	KieSession kSession1 = kContainer1.newKieSession();
  		//ClassLoader.get
//  		ks.newKieBuilder
  		KieBuilder kb=ks.newKieBuilder(kfs);
  		kb.buildAll();
  		System.out.println(kb.getResults().hasMessages(org.kie.api.builder.Message.Level.ERROR));
  		System.out.println(rule());
      	    KieContainer kContainer = ks.newKieContainer(kr.getDefaultReleaseId());
//          	KieSession kSession = kContainer.newKieSession("ksession-rules");

//          	
//              // go !kContainer.
//              Message message = new Message();
//              message.setMessage("Hello World");
//              message.setStatus(Message.HELLO);
//              BigDecimal m=new BigDecimal("1212");
//              m.add(new BigDecimal("1111"));
//              FactHandle l=     kSession.insert(message);
////              kSession.fireAllRules();
////              Message message1 = new Message();
////              kSession.delete(l);
////              kSession.insert(message);
////              kSession.fireAllRules();
//              
          	KieSession kSession2 = kContainer.newKieSession();
              
              BillParms billParams=new BillParms();
              billParams.setType("1");
              Map <String,String> ppp=new HashMap<String,String>();
              ppp.put("bbbb", "232323");
              billParams.setParams(ppp);
//              kSession2.insert(billParams);
//              kSession2.fireAllRules();
              kSession1.insert(billParams);
              kSession1.fireAllRules();
              
          } catch (Throwable t) {
              t.printStackTrace();
          }
    }
    
	public static String rule1(){
		
		String rule="package com.orangelife.service.bill ";
		 

 rule+="import com.orangelife.service.drool.BillParms;";


		 rule+="rule \"Hello World\" ";
		 rule+="no-loop true "; 
		 rule+="when ";
		 rule+=     "$m : BillParms()";
				 rule+=  " then ";
				 rule+=  "System.out.println(1000000);";
		   
		     
						 rule+=	"end";

	
return rule;
		
	}
	public static String rule(){
		
		String rule="package com.orangelife.service.bill ";
		 

 rule+="import com.orangelife.service.drool.BillParms;";


		 rule+="rule \"Hello World\" ";
		 rule+="no-loop true "; 
		 rule+="when ";
		 rule+=     "$m : BillParms()";
				 rule+=  " then ";
				 rule+=  "System.out.println(222221111);";
		   
		     
						 rule+=	"end";

	
return rule;
		
	}
    public void bb(){
    	
    	  try {
      		
              // load up the knowledge base
  	        KieServices ks = KieServices.Factory.get();
      	    KieContainer kContainer = ks.getKieClasspathContainer();
          	KieSession kSession = kContainer.newKieSession("ksession-rules");
      
          	
          	
              // go !kContainer.
              Message message = new Message();
              message.setMessage("Hello World");
              message.setStatus(Message.HELLO);
              BigDecimal m=new BigDecimal("1212");
              m.add(new BigDecimal("1111"));
              kSession.insert(message);
              kSession.fireAllRules();
         
          } catch (Throwable t) {
              t.printStackTrace();
          }
    	
    }

    public static class Message {

        public static final int HELLO = 0;
        public static final int GOODBYE = 1;
         private Map<String,Object>params=new HashMap<String,Object>();
        private String message;

        private int status;

        public String getMessage() {
            return this.message;
        }

        public void setMessage(String message) {
            this.message = message;
        }

        public int getStatus() {
            return this.status;
        }

        public void setStatus(int status) {
            this.status = status;
        }

		public Map<String,Object> getParams() {
			return params;
		}

		public void setParams(Map<String,Object> params) {
			this.params = params;
		}

    }

}
